package com.example.weather_forecast;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private CityManager cityManager;
    private EditText cityNameEditText;
    private Button addCityButton, removeCityButton, showCitiesButton;
    private TextView citiesTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);  // 保留边缘处理设置
        setContentView(R.layout.activity_main);

        cityManager = new CityManager(this);
        cityNameEditText = findViewById(R.id.cityNameEditText);
        addCityButton = findViewById(R.id.addCityButton);
        removeCityButton = findViewById(R.id.removeCityButton);
        showCitiesButton = findViewById(R.id.showCitiesButton);
        citiesTextView = findViewById(R.id.citiesTextView);

        // 边缘处理
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // Add city functionality
        addCityButton.setOnClickListener(v -> {
            String cityName = cityNameEditText.getText().toString();
            if (!cityName.isEmpty()) {
                cityManager.addCity(cityName);
                Toast.makeText(this, "City added: " + cityName, Toast.LENGTH_SHORT).show();
            }
        });

        // Remove city functionality
        removeCityButton.setOnClickListener(v -> {
            String cityName = cityNameEditText.getText().toString();
            if (!cityName.isEmpty()) {
                if (cityManager.findCityByName(cityName)) {
                    cityManager.removeCity(cityName);
                    Toast.makeText(this, "City removed: " + cityName, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "City not found!", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // Show cities functionality
        showCitiesButton.setOnClickListener(v -> {
            List<String> cities = cityManager.loadCities();
            StringBuilder stringBuilder = new StringBuilder();
            for (String city : cities) {
                stringBuilder.append(city).append("\n");
            }
            citiesTextView.setText(stringBuilder.toString());
        });
    }
}




